<div id="chat-container" class="chat-container">

	
</div>
<input type="text" id="chat-message" class="chat-message" value="">
<div id="chat-send" class="chat-send">Enter</div>


<script>
	{literal}
	$(document).ready( function ()
	{
		
		function empty (mixed_var) {
		    // http://kevin.vanzonneveld.net
		    // +   original by: Philippe Baumann
		    // +      input by: Onno Marsman
		    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		    // +      input by: LH
		    // +   improved by: Onno Marsman
		    // +   improved by: Francesco
		    // +   improved by: Marc Jansen
		    // +   input by: Stoyan Kyosev (http://www.svest.org/)
		    // *     example 1: empty(null);
		    // *     returns 1: true
		    // *     example 2: empty(undefined);
		    // *     returns 2: true
		    // *     example 3: empty([]);
		    // *     returns 3: true
		    // *     example 4: empty({});
		    // *     returns 4: true
		    // *     example 5: empty({'aFunc' : function () { alert('humpty'); } });
		    // *     returns 5: false
		    
		    var key;
		    
		    if (mixed_var === "" ||
		        mixed_var === 0 ||
		        mixed_var === "0" ||
		        mixed_var === null ||
		        mixed_var === false ||
		        typeof mixed_var === 'undefined'
		    ){
		        return true;
		    }
		
		    if (typeof mixed_var == 'object') {
		        for (key in mixed_var) {
		            return false;
		        }
		        return true;
		    }
		
		    return false;
		}		
		
		var last_chat_id = 0;
		var chat_update_interval = 1000;
		
		function update_chat_populate( s )
		{
			var i = 0;
			var new_line = '';
			last_chat_id = parseInt( s['data']['chat_lines']['last_chat_id'] );
			for ( i = 0 ; i < s['data']['chat_lines']['lines'].length ; i++ )
			{
				if ( s['data']['chat_lines']['lines'][i]['id_User'] != {/literal}{$ego_user_id}{literal} || 1 )
				{
					new_line = '<div class="chat-username">' +
									s['data']['chat_lines']['lines'][i]['Username'] +
								'</div>' +
								'<div class="chat-line">' +
									s['data']['chat_lines']['lines'][i]['Mesaj'] +
								'</div><br />';
					$('#chat-container').append( new_line );
				}
			}	
			$('#chat-container').scrollTo( 'max' );
		}
		
		var chat_last_update_time = 0;
		var chat_update_ajax_handle = 0;
		
		function chat_check_update()
		{
			var now_time = new Date();
			if ( ( now_time.getTime() - chat_last_update_time.getTime() ) > ( chat_update_interval * 2 ) )
			{
				clearTimeout( chat_refresh );
				chat_update_ajax_handle.abort();
				update_chat();
			}
		}
		
		function update_chat()
		{
			chat_last_update_time = new Date();
			var p = 'last_chat_id=' + last_chat_id;
			chat_update_ajax_handle = $.post(
				'/chat/get',
				p,
				function ( s, t )
				{
					if ( s['error'] == 'OK' )
					{
						if ( parseInt( s['data']['chat_lines']['last_chat_id'] ) > parseInt( last_chat_id ) )
						{
							update_chat_populate( s );
						}
					}
					clearTimeout( chat_refresh );
					chat_refresh = setTimeout( function() { update_chat(); }, chat_update_interval );
				},
				"json"	
			);
			
		}
		
		var chat_refresh = 0;
		
		function chat_send_message( e )
		{
			var text = $('#chat-message').val();
			if ( empty( text ) )
			{
				return false;
			}
			else
			{
				var new_line = '<div class="chat-username">' +
								{/literal}'{$ego_user_name}'{literal} +
							'</div>' +
							'<div class="chat-line">' +
								text +
							'</div><br />';
				//$('#chat-container').append( new_line );
				$('#chat-container').scrollTo( 'max' );				
				
				$('#chat-message').val('');
				var p = 'text=' + encodeURIComponent( text ) +
						'&last_chat_id=' + last_chat_id;
				
				$.post(
					'/chat/send',
					p,
					function( s, t )
					{
						if ( s['error'] == 'OK' )	
						{
							if ( s['data']['chat_lines']['last_chat_id'] > last_chat_id )
							{
								update_chat_populate( s );
								update_chat();
							}
						}
					},
					"json"
				);
			}	
		}
		$('#chat-send').click( chat_send_message );
		$('#chat-message').keydown( function( e )
		{
			if ( e.keyCode == 13 )	
			{
				e.preventDefault();
				chat_send_message();
				return false;
			}
		} );
		
		update_chat();
		
		setInterval( function() { chat_check_update(); }, chat_update_interval * 2 );
	} );
	{/literal}
</script>

